package com.parenting.controller;


import cn.hutool.core.bean.BeanUtil;
import com.parenting.entities.ParentingShopDO;
import com.parenting.model.IMPage;
import com.parenting.oauth2.config.AuthUtil;
import com.parenting.request.*;
import com.parenting.response.ShopResponse;
import com.parenting.response.UnclaimedListResponse;
import com.parenting.service.ParentingShopService;
import com.parenting.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 店铺主表 前端控制器
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-08-18
 */
@RestController
@Api(tags = "店铺管理接口", description = "店铺（曾、改、查、店铺未认领列表、店铺认领）")
@RequestMapping("/shop")
public class ParentingShopController {

    @Autowired
    private ParentingShopService parentingShopService;

    /****
     *  新增店铺
     * @param shop  店铺实体类
     */
    @ApiOperation("新增店铺")
    @PostMapping
    public Result saveShop(@RequestBody ShopSaveRequest shop){
       parentingShopService.saveShop(AuthUtil.getUserInfo(),shop);
       return  Result.success();
   }

    /***
     * 修改店铺
     * @param shop 店铺对象
     * @return
     */
    @ApiOperation("修改店铺")
    @PutMapping
    public Result updateShop(@RequestBody ShopUpdateRequest shop){
        parentingShopService.updateShop(shop);
        return  Result.success();
    }

    /***
     * 未认领的店铺列表 查询所有为认领的店铺
     * @return
     */
    @ApiOperation("未认领店铺列表")
    @PostMapping("unclaimed")
    public Result<IMPage<UnclaimedListResponse>> ListOfUnclaimedShops(@RequestBody @Validated UnclaimedListConditionRequest unclaimedListCondition){
        IMPage<UnclaimedListResponse> iPage = parentingShopService.ListOfUnclaimedShops(unclaimedListCondition);
        return Result.buildSuccess(iPage);
    }

    /***
     * 店铺认领
     * @param shop
     */
    @ApiOperation("认领店铺")
    @PostMapping("claim")
    public Result ClaimShop(@RequestBody @Validated ShopAutRequest shop){
        parentingShopService.ClaimShop(AuthUtil.getUserInfo(),shop);
        return  Result.success();
    }



    @ApiOperation("获得当前登录用户的店铺详情")
    @GetMapping
    public Result<ShopResponse> getShopByLoginUserId(){
        String shopId = AuthUtil.getUserInfo().getShopId();
        ParentingShopDO byId = parentingShopService.getById(shopId);
        ShopResponse shopResponse = BeanUtil.copyProperties(byId, ShopResponse.class);
        return  Result.buildSuccess(shopResponse);
    }


    @ApiOperation("校验店铺是否存在")
    @PostMapping("chek")
    public Result chekShop(@RequestBody  ChekShopRequest shop){

        return Result.ok(parentingShopService.chekShop(shop));
    }

}

